iT邦幫忙

2023 iThome 鐵人賽

DAY 6
1
自我挑戰組

ESP32 Micropython 從入門到實用系列 第 6

06.更新開發板的 Micropython 韌體(下)

  • 分享至 

  • xImage
  •  

今天要來介紹文字模式,命令列下指令的方式來燒錄 ESP32 韌體。這些步驟有點繁瑣,初學者看不懂沒有關係,不一定要學會這種燒錄方法,用前兩天學到的方法已經足夠!

Espressif 官方有出一個 Python 套件的燒錄程式 --- esptool,我們以 BPI-leaf-S3 的燒錄爲例,來說明 Micropython 韌體的更新。

開啓帶有 esptool 之 Python 虛擬環境終端,以下方法任一種即可:

  • 若是您用執行 Thonny 傻瓜安裝, 則在 Thonny 的 Tools 中打開 "Open system shell"
  • 若是您 Thonny 是用虛擬環境安裝,就打開帶有那個虛擬環境的 win11 cmd/powershell/Terminal 終端

在這裡必須注意一點的是,要使用 esptool 工具,必須讓板子置於 “燒錄模式”,這是因爲我們連接到個人電腦,使用的是 ESP32 原生的 USB CDC port,因此無法透過一般常用的 DTR/RTS 來達成進入自動燒錄模式。

還記得我們昨天提到,進入 Leaf-S3 板子進入燒錄模式的方法嗎?

簡要再說明如下:板子上電,按住板子上的 BOOT 按鈕不放,按一下 RST(Reset) 按鍵後放開,再放開 BOOT 按鍵。

那我們怎樣才知道已經進入燒錄模式呢?

這裡我們偷偷的教您使用 mpremote 工具: (在 Python 的虛擬環境終端)

mpremote devs

mpremote devs, 可以幫我們列出目前所有的 Serial port

以我這裡爲例的結果如下:

未進入燒錄模式的情況下

X:\MPY\network>mpremote devs
COM4 None 0000:0000 Microsoft None
COM5 None 0000:0000 Microsoft None
COM9 123456 303a:4001 Microsoft None

已進入燒錄模式的情況下

X:\MPY\network>mpremote devs
COM4 None 0000:0000 Microsoft None
COM5 None 0000:0000 Microsoft None
COM8 48:27:E2:0D:6F:04 303a:1001 Microsoft None

您有看到什麼不一樣的地方嗎? 明顯的 COM9 變成 COM8,但這並不重要,重要的是 303a 是 Espressif 公司擁有的 USB VID (Vender ID),1001 是燒錄模式下 Serial port 的 USB PID (Product ID), Bingo!!! 看到這裡,就知道我們已經成功進入燒錄模式了。

然後在命令模式輸入 esptool , 執行結果如下:

X:\MPY\network>esptool
esptool.py v4.6.2
usage: esptool [-h]
               [--chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2}]
               [--port PORT] [--baud BAUD] [--before {default_reset,usb_reset,no_reset,no_reset_no_sync}]
               [--after {hard_reset,soft_reset,no_reset,no_reset_stub}] [--no-stub] [--trace]
               [--override-vddsdio [{1.8V,1.9V,OFF}]] [--connect-attempts CONNECT_ATTEMPTS]
               {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,merge_bin,get_security_info,version}
               ...

esptool.py v4.6.2 - Espressif chips ROM Bootloader Utility

positional arguments:
  {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,merge_bin,get_security_info,version}
                        Run esptool.py {command} -h for additional help
    load_ram            Download an image to RAM and execute
    dump_mem            Dump arbitrary memory to disk
    read_mem            Read arbitrary memory location
    write_mem           Read-modify-write to arbitrary memory location
    write_flash         Write a binary blob to flash
    run                 Run application code in flash
    image_info          Dump headers from a binary file (bootloader or application)
    make_image          Create an application image from binary files
    elf2image           Create an application image from ELF file
    read_mac            Read MAC address from OTP ROM
    chip_id             Read Chip ID from OTP ROM
    flash_id            Read SPI flash manufacturer and device ID
    read_flash_status   Read SPI flash status register
    write_flash_status  Write SPI flash status register
    read_flash          Read SPI flash content
    verify_flash        Verify a binary blob against flash
    erase_flash         Perform Chip Erase on SPI flash
    erase_region        Erase a region of the flash
    merge_bin           Merge multiple raw binary files into a single file for later flashing
    get_security_info   Get some security-related data
    version             Print esptool version

options:
  -h, --help            show this help message and exit
  --chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2}, -c {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2}
                        Target chip type
  --port PORT, -p PORT  Serial port device
  --baud BAUD, -b BAUD  Serial port baud rate used when flashing/reading
  --before {default_reset,usb_reset,no_reset,no_reset_no_sync}
                        What to do before connecting to the chip
  --after {hard_reset,soft_reset,no_reset,no_reset_stub}, -a {hard_reset,soft_reset,no_reset,no_reset_stub}
                        What to do after esptool.py is finished
  --no-stub             Disable launching the flasher stub, only talk to ROM bootloader. Some features will not be
                        available.
  --trace, -t           Enable trace-level output of esptool.py interactions.
  --override-vddsdio [{1.8V,1.9V,OFF}]
                        Override ESP32 VDDSDIO internal voltage regulator (use with care)
  --connect-attempts CONNECT_ATTEMPTS
                        Number of attempts to connect, negative or 0 for infinite. Default: 7.

X:\MPY\network>

沒有輸入任何參數,esptool 會印出 help 畫面,簡要的列出其使用方式。東西一大堆,或許您一時之間嚇到了!您不用緊張,我們會從簡單的入門,其它的部分你再慢慢看,看久了,自然就懂了,若有任何問題,也歡迎您留言提問。

若是您的系統反應的是,找不到 esptool.ese 等之類的訊息,您可以改用

python -m esptool

來取代 esptool.exe 命令。

若是我們想要檢查開發板上的 ESP 晶片 ID,我們可以下 “esptool chip_id":

X:\MPY\network>esptool chip_id
esptool.py v4.6.2
Found 3 serial ports
Serial port COM8
Connecting...
Detecting chip type... ESP32-S3
Chip is ESP32-S3 (revision v0.1)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 48:27:e2:0d:6f:04
Uploading stub...
Running stub...
Stub running...
Warning: ESP32-S3 has no Chip ID. Reading MAC instead.
MAC: 48:27:e2:0d:6f:04
Hard resetting via RTS pin...

esptool告訴我們,發現了三個 Serial port, 在 COM8 是 ESP32-S3 開發板!
我們在下指令的時候,也可以指定是哪個 Serial port、哪個速度:

X:\MPY\network>esptool -p COM8 -b 460800 chip_id
esptool.py v4.6.2
Serial port COM8
Connecting...
Detecting chip type... ESP32-S3
Chip is ESP32-S3 (revision v0.1)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 48:27:e2:0d:6f:04
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Warning: ESP32-S3 has no Chip ID. Reading MAC instead.
MAC: 48:27:e2:0d:6f:04
Hard resetting via RTS pin...

在這裏: -p 指明是對 COM8 ; -b 指明速度是 460800 baud

用 esptool 工具抹除 flash

esptool -p COM8 -b 460800 erase_flash

結果如下:

X:\MPY\network>esptool -p COM8 -b 460800 erase_flash
esptool.py v4.6.2
Serial port COM8
Connecting...
Detecting chip type... ESP32-S3
Chip is ESP32-S3 (revision v0.1)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 48:27:e2:0d:6f:04
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Erasing flash (this may take a while)...
Chip erase completed successfully in 18.7s
Hard resetting via RTS pin...

表示已經抹除完成!

用 esptool 工具燒錄 UF2 bootloader 韌體

下載昨天文章中的 leaf-S3 UF2 bootloader 韌體,並解開壓縮檔,這裡爲方便解說,我把其中要燒錄的 combine.bin 韌體複製到 用戶的 Downloads 目錄,然後下:

esptool -p COM8 -b 460800 write_flash 0x0 c:\Users\ts\Downloads\combined.bin

這裡,write_flash 是燒錄指令,從0x0 地址開始燒錄 (0x0 就是 0 的十六進位表示), 然後指明要的燒錄檔案。
執行結果如下:

X:\MPY\network>esptool -p COM8 -b 460800 write_flash 0x0 c:\Users\ts\Downloads\combined.bin
esptool.py v4.6.2
Serial port COM8
Connecting...
Detecting chip type... ESP32-S3
Chip is ESP32-S3 (revision v0.1)
Features: WiFi, BLE
Crystal is 40MHz
MAC: 48:27:e2:0d:6f:04
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00437fff...
Compressed 4422048 bytes to 120964...
Wrote 4422048 bytes (120964 compressed) at 0x00000000 in 27.7 seconds (effective 1278.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

表示 UF2 bootloader 已經燒錄成功

燒錄 Micropython UF2 韌體

這個步驟就簡單了!

  • 讓leaf-S3板子進入UF2 bootloader 模式:
    按一下 RST 按鍵,然後在 0.5 ~ 1 秒左右,按一下 BOOT 按鍵,這時會產生一個 USB (LEAFS3BOOT) 磁碟

  • 將要燒錄的 Micropython 韌體 拖拉到那個 USB 碟 來燒錄韌體。

如此,我們 leaf-S3 開發板就從預載的 傳統 Micropython 韌體,更新爲 UF2 格式的 Micropython 韌體了

esptool 的補充說明

esptool 是 ESP 晶片 最重要,也是最常用的韌體燒錄工具,但很難想像,這樣好用的軟體,一開始竟然不是 Espressif 公司所開發的,反而是開源社群的貢獻,而且還分支了一些不同的版本,當然最後也是受到原廠公司大力的支持,整合到官方成爲正式的燒錄軟體,終結衆多版本的亂象,而原廠對於社群也十分友善,功能也越來越完善。

ESP Web flasher 可以看作是 Web 版的 esptool 燒錄程式,Adafruit 也是在贊助開源開發者的模式下開發完成,後來 Espressif 官方的 esptool.js web 燒錄軟體,大約比 Adafruit 晚半年至一年啓動開發。去年初,Adafruit ESP Web flasher 的程式維護宣佈放棄繼續支持,請大衆改用 Espressif 官方的 esptool.js。然而因爲 那時esptool.js 的問題極多,Adafruit 又重新接手 ESP Web flasher 的維護,開源社群的運作讓我們能享用這美好方便的工具。

我們購買的開發板,需要軟體的支援,開發系統會不斷的更新,Espressif 開源的態度,甚至讓我們在近 10 年前買的 ESP8266 開發板都還能得到完整的支持,而不至於成爲孤兒,這樣的巨量維護工作,不是原廠能夠負擔的,這有賴與開源社群高手的進來幫忙。Espressif 開放的態度,着實吸引了不少高手進來,而 Espressif 甚至網羅開源社群的一些高手來經營技術網站與相關的技術開發,形成正循環,這一點是我們臺灣廠商應該學習的,要正確的善用開源社群資源!


上一篇
05.更新開發板的 Micropython 韌體(中)
下一篇
07.Python 入門學習途徑
系列文
ESP32 Micropython 從入門到實用30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言